package com.crayon2f.test.version.jdk16;

import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.lang.Thread;
import java.util.concurrent.TimeUnit;
public class Main {
    
    private static final CountDownLatch CDL = new CountDownLatch(1);
    private static int STATUS = 1;
    
    public static void main(String[] args) throws InterruptedException {
        
        Thread thread1 = new Thread(() -> {
            try {
                while(true){
                    while(STATUS == 1){
                        CDL.countDown();
                        System.out.println("t1");
                        STATUS = 2;
                        CDL.await();
                    }
                }   
            }catch(Exception e){
                e.printStackTrace();
            } 
        });
        Thread thread2 = new Thread(() -> {
            try {
                while(true){
                    while(STATUS == 2){
                        CDL.await();
                        System.out.println("t2");
                        STATUS = 1;
                        TimeUnit.SECONDS.sleep(2);
                        CDL.countDown();
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        });
        thread1.start();
        thread2.start();
        thread2.join();
    }
}